<?php

require_once("db_functions.php");
require_once("page.class.php");

class banke extends page{
	
	var $db;
	
	function banke(){
		$this->db = new db_functions();
		
		$a = getFromInput("a");
		$mod = getFromInput("mode");
		switch ($a){
			case "new":
				$this->novi();
				break;
			case 'edit':
				$this->edit();
				break;
			case "save":
				$this->save();
				break;
			case "delete":
				$this->delete();
				break;
			default:
				$this->browse();
				break;
		}
	}
	
	function novi(){
		$this->assign("title","Nova banka");
		echo $this->parseTemplate("banke/bank.tpl");
	}
	
	function save(){
		$id		= trim(getFromInput("bank_id"));
		$code	= trim(getFromInput("bank_code"));
		$name	= trim(readValFromInput("bank_name"));
		$adr	= trim(getFromInput("bank_adr"));
		$acc	= trim(getFromInput("bank_acc"));
		
		if ($id>0) {	// edit
			
			$q = "UPDATE banke SET "
				."banka_code=$code,"
				."banka_name='".addslashes($name)."',"
				."banka_adresa='".addslashes($adr)."',"
				."banka_zracun='$acc' "
				."WHERE banka_id=$id LIMIT 1";
			
		}else{			// new
			$q = "INSERT INTO banke(banka_code,banka_name,banka_adresa,banka_zracun) "
				."VALUES($code,'".addslashes($name)."','".addslashes($adr)."','$acc')";
		}
		
		$code = 1; $msg = "Banka je sačuvana.";
		if (!$this->db->alterTable($q)) {
			$code = 0;
			$msg = mysql_error();
		}
		
		echo "$code{!}$msg";
	}
	
	function delete(){
		$id = getFromInput("id");
		
		$code=1; $msg="Banka je obrisana.";
		
		$q = "DELETE FROM banke WHERE banka_id=$id LIMIT 1";
		if(!$this->db->alterTable($q)){
			$code=0;
			$msg=mysql_error();
		}
		
		echo "$code{!}$msg";
		
	}
	
	function edit(){
		$id = getFromInput("id");
		
		$q = "SELECT * FROM banke WHERE banka_id=$id LIMIT 1";
		list($res) = $this->db->select2($q);
		
		$this->assign("bank_id",$res["banka_id"]);
		$this->assign("bank_code",$res["banka_code"]);
		$this->assign("bank_name",$res["banka_name"]);
		$this->assign("bank_adr",$res["banka_adresa"]);
		$this->assign("bank_acc",$res["banka_zracun"]);
		
		$this->assign("title","Izmena banke #$id");
		
		echo $this->parseTemplate("banke/bank.tpl");
	}
	
	function browse(){
		$fCode = trim(readValFromInput("filter_code"));
		$fName = trim(readValFromInput("filter_name"));
		
		$this->assign("filter_code",$fCode);
		$this->assign("filter_name",$fName);
		
		$q = "SELECT * FROM banke WHERE 1";
		$all = count($this->db->select1($q));
		if (strlen($fCode)>0) {
			$q .= " AND banka_code LIKE '%$fCode%'";
		}
		if (strlen($fName)>0) {
			$q .= " AND banka_name LIKE '%$fName%'";
		}
		$q .= " ORDER BY banka_name";
		
		$res = $this->db->select2($q);
		if ($res !== false) {
			$this->assign("info","Ukupno: ".count($res)."/$all");
			foreach ($res as $row){
				$ids[] = $row["banka_id"];
				$codes[] = $row["banka_code"];
				$names[] = $row["banka_name"];
				$adrs[] = $row["banka_adresa"];
				$zrs[] = $row["banka_zracun"];
			}
			$this->assign("bank_id",$ids);
			$this->assign("bank_code",$codes);
			$this->assign("bank_name",$names);
			$this->assign("bank_address",$adrs);
			$this->assign("bank_acc",$zrs);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Nema rezultata.");
		}
		
		echo $this->parseTemplate("banke/browse.tpl");
	}
}



?>